Why doesn't `stdin.read()` read entire buffer?
Posted
by
Shookie
on Stack Overflow
See other posts from Stack Overflow
or by Shookie
Published on 2014-08-25T10:16:19Z
Indexed on
2014/08/25
10:19 UTC
Read the original article
Hit count: 173
I've got the following code:
def get_input(self):
"""
Reads command from stdin, returns its JSON form
"""
json_string = sys.stdin.read()
print("json string is: "+json_string)
json_data =json.loads(json_string)
return json_data
It reads a json string that was sent to it from another process. The json is read from stdin. For some reason I get the following output:
json string is: <Some json here>
json string is:
Traceback (most recent call last):
File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 63, in <module>
manager.accept_commands()
File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 49, in accept_commands
json_data = self.get_input()
File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 42, in get_input
json_data =json.loads(json_string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
So for some reason it reads an empty string from stdin instead of reading only the json. I've checked, and the code that writes to this process's stdin writes to it only once.
What's wrong here?
© Stack Overflow or respective owner